<?php
	$strAddressType = "Credit Card";
	if((CARD_PROCESSING == false) && ($SHOP_TYPE == "Trade"))
		$strAddressType = "Company";
	if(!isset($SHOP_TYPE))
		$SHOP_TYPE = null;
	$intTotal 		= 0;
	$intVatTotal 	= 0;
	$intVATRate  	= 0; 
	$strFormName 	= "frmOrders";
	$strQueryName 	= "actProcessOrder";
	$strRefresh 	= "$strFormName.SCREEN.value='scrCheckout';
						$strFormName.ACTION.value='';
						$strFormName.submit()";
	//echo $_SESSION['sesCustID']." cust id 2< /br>";
	if( (isset($_REQUEST['txtOtherInfo'])) && ($_REQUEST['txtOtherInfo'] != "") )
		$txtOtherInfo = cleanFormInput($_REQUEST['txtOtherInfo']);
	else
		$txtOtherInfo = $strOtherInfoDefault;
		
	if(isset($_REQUEST['chkTerms']))
		$chkTerms = cleanFormInput($_REQUEST['chkTerms']);
	else 
		$chkTerms = null;
	
	if (isset($_SESSION['sesCustID']))
	    include (MODULE . "/action/qrySelectCustomer");

					 
	if(!isset($_REQUEST['strReprocessing']))
	{
	 	$txtShipName   =  $myrow["FirstName"]." ".$myrow["Surname"]; 	 
		$txtShipAddress = $myrow["Address1"]; 
		$txtShipAddress2 = $myrow["Address2"];	
		$txtShipCity     = $myrow["City"]; 
		$txtShipRegion   = $myrow["Region"];
		$txtShipPostalCode = $myrow["PostalCode"];			
		$txtShipCountry   = DisplayLookupValue("tblCountry","Country",$myrow['Country']);	
		$txtShipPhone     = $myrow['Phone'];		  
		//$txtShipMobile    = $myrow['Mobile']; 
		
		$txtDeliveryName   =  $myrow["FirstName"]." ".$myrow["Surname"]; 	 
		$txtDeliveryAddress = $myrow["Address1"]; 
		$txtDeliveryAddress2 = $myrow["Address2"];	
		$txtDeliveryCity     = $myrow["City"]; 
		$txtDeliveryRegion   = $myrow["Region"];
		$txtDeliveryPostalCode = $myrow["PostalCode"];			
		$txtDeliveryCountry   = DisplayLookupValue("tblCountry","Country",$myrow['Country']);	
		$txtDeliveryPhone     = $myrow["Phone"];		  
	}
echo "
<tr>
	<td valign='top'>
		".StartForm("scrDisplayOrder", "post", $strQueryName,array('strReprocessing'=>true),"","",$strFormName,"")."
		<table cellpadding='0' cellspacing='0' class='Normal' border='0' align='left' width='100%'>";
		if(PHONE != "")
		{
			echo "
			<tr>
				<td colspan='5' align='center'><h1>Tel: ".PHONE."</h1></td>
			</tr>";
		}

	echo "
			<tr class='ProductHeader' bgcolor='silver'>
				 <td width='7%' align='center' height='20' class='ProductHeader'><img src='./images".SITE_SUFFIX."/checkout.gif' width='20' height='20' border='0' alt=''></td><td colspan='3'  class='ProductHeader'>Current Order Checkout</td><td align='right'  class='ProductHeader'><img src='./images".SITE_SUFFIX."/bar_end_inverse.gif' width='23' height='25' border='0' alt=''></td>
			</tr>
		
			<tr>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td width='50'><img src='./images".SITE_SUFFIX."/space.gif' width='50' height='1' border='0' alt='' /></td>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
			</tr>
			
			 	<tr class='ProductSubHeader' bgcolor='#0066CC'>
			 			<td height='20'>&nbsp;</td>
						<td align='left' colspan='4'  class='ProductSubHeader'>
							$strAddressType Address
						</td>
				</tr>
				
			     <tr>
			 		<td colspan='5'>
						&nbsp;
					</td>
				</tr>
				
			 	<tr>
			 		<td>&nbsp;</td>
		         	<td align='right'>
						*Name :&nbsp;
					</td>
		         	<td colspan='3'>
						<input type=\"text\" name=\"txtShipName\" value = \"".$txtShipName."\" size= \"30\" maxlength=\"40\">
					</td>
		     	</tr>
		
		     	<tr>
			 		<td>
						&nbsp;
					</td>
		         <td align='right'>
				 	*Address :&nbsp;
				</td>
		         <td colspan='3'>
				 	<input type=\"text\" name=\"txtShipAddress\" value = \"".$txtShipAddress."\" size= \"30\" maxlength=\"60\">
				</td>
			</tr>
			 
		    <tr>
			 	<td>
					&nbsp;
				</td>
		        <td align='right'>
					&nbsp;:&nbsp;
				</td>
		        <td colspan='3'>
					<input type=\"text\" name=\"txtShipAddress2\" value = \"".$txtShipAddress2."\" size= \"30\" maxlength=\"60\">
				</td>
		    </tr>
			 
		    <tr>
			 	<td>
					&nbsp;
				</td>
		        <td align='right'>
					*Town/City :&nbsp;
				</td>
		        <td colspan='3'>
					<input type=\"text\" name=\"txtShipCity\" value = \"".$txtShipCity."\" size= \"25\" maxlength=\"30\">
				</td>
		    </tr>
		
		    <tr>
			 	<td>
					&nbsp;
				</td>
		         <td align='right'>
				 	County :&nbsp;
				</td>
		         <td colspan='3'>
				 	<input type=\"text\" name=\"txtShipRegion\" value = \"".$txtShipRegion."\" size= \"20\" maxlength=\"25\">
				</td>
		    </tr>
		
		    <tr>
				<td>
					&nbsp;
				</td>
		        <td align='right'>
					*Postal Code :&nbsp;
				</td>
		       	<td colspan='3'>
					<input type=\"text\" name=\"txtShipPostalCode\" value = \"".$txtShipPostalCode."\" size= \"10\" maxlength=\"10\">
				</td>
		     </tr>
		
		     <tr>
			 	<td>
					&nbsp;
				</td>
		        <td align='right'>
					*Ship Country :&nbsp;
				</td>
		        <td colspan='2'>
					<input type=\"text\" name=\"txtShipCountry\" value = \"".$txtShipCountry."\" size= \"15\" maxlength=\"15\">
				</td>
		     </tr>
			 
			 <tr>
				<td>&nbsp;</td>
				<td align='right'>*Phone :&nbsp;</td>
				<td colspan='3'><input type=\"text\" name=\"txtShipPhone\" value = \"".$txtShipPhone."\" size= \"15\" maxlength=\"15\"></td>
			</tr>
			
			<tr>
				<td>&nbsp;</td>
				<td align='right'>Mobile :&nbsp;</td>
				<td colspan='3'><input type=\"text\" name=\"txtShipMobile\" value = \"".$txtShipMobile."\" size= \"15\" maxlength=\"15\"></td>
			</tr>";
		
		//If seperate delivey address is permited the display form fields
		if(SEPARATE_DELIVERY_ADDRESS){
			 echo "
			 	<tr class='ProductSubHeader' bgcolor='#0066CC'>
		 			<td height='20'>&nbsp;</td>
					<td align='left' colspan='4'  class='ProductSubHeader'>
						Delivery Address
					</td>
				</tr>
				
				<tr>
					<td colspan='5'>&nbsp;</td>
				</tr>
		 		
				<tr>
					<td>&nbsp;</td>
				 	<td align='right'>*Name :&nbsp;</td>
					<td colspan='3'><input type=\"text\" name=\"txtDeliveryName\" value = \"".$txtDeliveryName."\" size= \"30\" maxlength=\"40\"></td>
				</tr>
				
				<tr>
					<td>&nbsp;</td>
				 	<td align='right'>*Address :&nbsp;</td>
					<td colspan='3'><input type=\"text\" name=\"txtDeliveryAddress\" value = \"".$txtDeliveryAddress."\" size= \"30\" maxlength=\"60\"></td>
			 	</tr>
				
				<tr>
				
					<td>&nbsp;</td>
					<td align='right'>&nbsp;:&nbsp;</td>
					<td colspan='3'><input type=\"text\" name=\"txtDeliveryAddress2\" value = \"".$txtDeliveryAddress2."\" size= \"30\" maxlength=\"60\"></td>";
		     echo "</tr>";
			 
		     echo "<tr>";
			 	echo "<td>&nbsp;</td>";
		         echo "<td align='right'>*Town/City :&nbsp;</td>";
		         echo "<td colspan='3'><input type=\"text\" name=\"txtDeliveryCity\" value = \"".$txtDeliveryCity."\" size= \"25\" maxlength=\"30\"></td>";
		     echo "</tr>";
		
		     echo "<tr>";
			 	echo "<td>&nbsp;</td>";
		         echo "<td align='right'>*County/State :&nbsp;</td>";
		         echo "<td colspan='3'><input type=\"text\" name=\"txtDeliveryRegion\" value = \"".$txtDeliveryRegion."\" size= \"20\" maxlength=\"25\"></td>";
		     echo "</tr>";
		
		     echo "<tr>";
			 	echo "<td>&nbsp;</td>";
		         echo "<td align='right'>*Postal Code :&nbsp;</td>";
		         echo "<td colspan='3'><input type=\"text\" name=\"txtDeliveryPostalCode\" value = \"".$txtDeliveryPostalCode."\" size= \"10\" maxlength=\"10\"></td>";
		     echo "</tr>";
		
		     echo "<tr>";
			 	echo "<td>&nbsp;</td>";
		         echo "<td align='right'>*Ship Country :&nbsp;</td>";
		         echo "<td colspan='2'><input type=\"text\" name=\"txtDeliveryCountry\" value = \"".$txtDeliveryCountry."\" size= \"15\" maxlength=\"15\"></td>";
		     echo "</tr>";
		
			echo "<tr>";
				echo "<td>&nbsp;</td>";
				echo "<td align='right'>*Phone :&nbsp;</td>";
				echo "<td colspan='3'><input type=\"text\" name=\"txtDeliveryPhone\" value = \"".$txtDeliveryPhone."\" size= \"15\" maxlength=\"15\"></td>";
			echo "</tr>";
		}
	echo "<tr><td colspan='5'>&nbsp;</td></tr>";

				//Display extra field to be added to order header.
				if(EXTRA_ORDER_FIELD){
					echo "
					 	<tr class='ProductSubHeader' bgcolor='#0066CC'>
				 			<td class='ProductSubHeader' height='20'>&nbsp;</td>
							<td class='ProductSubHeader' align='left' colspan='4'  class='ProductSubHeader'>
								Other Info
							</td>
						</tr>
						
						<tr>
							<td colspan='5'>&nbsp;</td>
						</tr>
						
						<tr>
				 			<td height='20'>
								&nbsp;
							</td>
							<td align='right'>
								*".EXTRA_ORDER_FIELD."&nbsp;:&nbsp;
							</td>
							<td align='left' colspan='3'>
								<input type=\"text\" name=\"txtOtherInfo\" value = \"".$txtOtherInfo."\" size= \"40\" maxlength=\"100\">
							</td>
						</tr>
			
						<tr>
							<td colspan='5'>&nbsp;</td>
						</tr>
						
					";
			    }
	
				if(CARD_PROCESSING){
					/*************  Card procssing  *********************/
				
					echo "
					
					
					<tr  class='ProductSubHeader' bgcolor='#0066CC'>
						<td>&nbsp;</td>
						<td align='left' colspan='4'  class='ProductSubHeader' bgcolor='#0066CC'>Credit Card Details</td>
					</tr>
					
					<tr>
						<td colspan='5'>";
					
						include(MODULE . "/screen/scrEnterCardDetails");
						/*************  Card procssing  *********************/
						
					echo "
						</td>
					</tr>";
				}


		echo "
			<tr>
				<td colspan='5' align='right'>* Required Information To Process Order.</td>
			</tr>
	 
	 		<tr>
				<td colspan='5'>&nbsp;</td>
			</tr>
	

			<tr class='ProductSubHeader' bgcolor='#0066CC'>
				<td class='ProductSubHeader' align='left'>&nbsp;</td>
				<td class='ProductSubHeader' align='left'>Product</td> 
				<td class='ProductSubHeader' align='left'>&nbsp;</td>
				<td class='ProductSubHeader' align='right'>Qty</td>
				<td class='ProductSubHeader' align='right'>Total</td>
			</tr>";
		
		
			 $session_array = explode(";",session_encode()); 
			for ($x = 0; $x < count($session_array); $x++) 
			{ 
				if (substr($session_array[$x],0,7)=="sesItem")
				{
					//if (eregi("(sesItem)([0-9]{1,9})(\|i\:)([0-9]{1,9})$",$session_array[$x], $OrderDetail)){	
					if (eregi("(sesItem)([0-9]{1,9})(\_)([0-9]{1,9})(\_)([0-9]{1,9})(\|i\:)([0-9]{1,9})$",$session_array[$x], $OrderDetail))
					{	
						//if (eregi("(sesItem)([0-9]{1,9})([^\"]*)([^a-z])([^\"]*)\"$",$session_array[$x], $OrderDetail)){			
						$boEmptyBasket = 'false';
						$strSQL = "SELECT 	tblProduct.Product 
											, tblProduct.QuantityPerUnit
											, tblProduct.UnitPrice
											, tblProduct.PromotionalItem
											, tblVatRate.VatRate 						\"VatRate\"
										FROM tblProduct
									LEFT JOIN 
										tblVatRate ON 
						 				tblProduct.VatRate = tblVatRate.ID
									WHERE tblProduct.ID =  '$OrderDetail[2]'";
						include (MODULE . "/action/actMySqlResults");				 
							
						if($myrow["PromotionalItem"] == "1")
						{
							// $strBackgroundImage = "background='./images/PromotionalItem.jpg'";	
							if(isset($strPromotionalFlag))
								$strPromotional = $strPromotionalFlag;
							else
								$strPromotional = "";
						}else{
						  $strPromotional = "";
						} 
							
						$intSubTotal = $myrow["UnitPrice"] * $OrderDetail[8];
						$intTotal = $intTotal + $intSubTotal;
						
						if($SHOP_TYPE == "Trade")
						{
							//echo "intSubTotal : $intSubTotal";
							$intVatSubTotal = ($intSubTotal * ($myrow["VatRate"]/100));
							//echo "intVatSubTotal : $intVatSubTotal";
							$intVatTotal = $intVatTotal + $intVatSubTotal;
							//echo "intVatTotal : $intVatTotal";
							$intVATRate = $myrow["VatRate"];
							$intVATCarriage = number_format((CARRIAGE - (CARRIAGE / (($intVATRate/100)+1))), 2, '.', '');
						}
						
						echo "<tr class='CheckoutProductDetail' valign='top' bgcolor=\"".switch_background($boBG)."\">";
							echo "<td align=right >";
								//if ($myrow["QuantityPerUnit"]){
									//echo $myrow["QuantityPerUnit"]."&nbsp;";
								//} else {
									echo "&nbsp;";
								//}
							echo "</td>";
							echo "<td align='left' colspan='2'>".$myrow["Product"].$strPromotional;
							
							if($myrow["QuantityPerUnit"]>1)
								echo "&nbsp;(".$myrow["QuantityPerUnit"]."&nbsp;pack)";

							if($OrderDetail[4] > 0)
								echo "<br/> Colour: ".DisplayLookupValue("tblColour","Colour",$OrderDetail[4]);

							
								if($OrderDetail[6] > 0){
									echo "<br/> Size: ".DisplayLookupValue("tblSize","Size",$OrderDetail[6]);
								}
							echo "</td>
								<td align='right'>".$OrderDetail[8]."</td>";
							echo "<td align='right'><b>".CURRENCY. number_format($intSubTotal, 2, '.', '')."</b></td>";
						echo "</tr>";
						
						$boBG = -1 - $boBG; //alternate $boBG values from -1 and 0
					}
				}
			} 
				
		echo "
		<tr>
			<td colspan='5'><hr></td>
		</tr>	
		
		<tr>
			<td align='right' colspan='4'>&nbsp;</td>
			<td align='right'>Carriage&nbsp;";
			
			
			if (CARRIAGE !== "0.00"){
					if($SHOP_TYPE == "Trade"){
						$intNetCarriage = number_format(CARRIAGE, 2, '.', '');
						$intVATCarriage = number_format((CARRIAGE * ($intVATRate/100)), 2, '.', '');
						$intTotalCarriage = number_format((CARRIAGE + $intVATCarriage), 2, '.', '');
						$intTotal = $intTotal + $intTotalCarriage;
					}else{
						$intNetCarriage = number_format(CARRIAGE / (($intVATRate/100)+1), 2, '.', '');
						$intVATCarriage = number_format((CARRIAGE - (CARRIAGE / (($intVATRate/100)+1))), 2, '.', '');
						$intTotalCarriage = number_format(CARRIAGE, 2, '.', '');
						$intTotal = $intTotal + $intTotalCarriage;
					}
					
				echo "<b>".CURRENCY.number_format(CARRIAGE, 2, '.', '')."</b>";
				
			}else{
				$intNetCarriage = '0';
				$intVATCarriage = '0';
				echo "<span class='Small'>Included</span>";
			}
			
	echo "
			</td>
		</tr>";
	
			if($SHOP_TYPE == "Trade"){
				echo "
				<tr>
					<td align='right' colspan='4'>&nbsp;</td> 
					<td align='right'>VAT&nbsp;<b>".CURRENCY. number_format($intVatTotal + $intVATCarriage, 2, '.', '')."</b></td>
				</tr>";
			
			}
		
		echo "
		<tr>
			<td align='right' colspan='4'>&nbsp;</td>
			<td align='right'><b>Total&nbsp;".CURRENCY. number_format($intTotal +  $intVatTotal, 2, '.', '')."</b></td>
		</tr>
		<tr>	
			<td colspan='5'>&nbsp;</td>
		</tr>
		
		<tr>
			<td colspan='5'><HR></td>
		</tr>
";
		//<?php ;
?>

    <tr>
         <td align="center" colspan="4">
             <b>Please tick the box to confirm agreement of the Terms and Conditions and continue with order.</b> 
             <br /><br /> I have <b>read</b> and <b>agreed</b> 
			 <u>
			 	<strong>
			 		<a href="javascript:;" onclick="MM_openBrWindow('<?php echo ScreenURL("Terms_review");?>','Terms','scrollbars=yes,menubar=no,toolbar=no,width=<?php echo TermsWidth;?>,height=500') ">
			 			Terms &amp; Conditions
			 		</a>
			 	</strong>
			 </u>.
		 	<input type="checkbox" name="chkTerms" value="1" <?=$chkTerms == 1?" checked='checked'":"";?>/>

         </td>
         <td>&nbsp;</td>
     </tr>



<?php
		//if($chkTerms == TRUE){	   
			//$chkTerms = TRUE;
				 if($boEmptyBasket + $intVatTotal == 'false' ){
					if($intTotal > MINIMUM_ORDER){
						echo "
						<tr>
							<td colspan='4'>
								<p align='center'>".CHECKOUT_MESSAGE."</p>
							</td>
							<td align='right'>
								<input type='image' src='./images".SITE_SUFFIX."/submitorder.gif' alt='submit order' onClick=\"$strFormName.ACTION.value='actProcessOrder';$strFormName.submit()\">
								<!-- <input type=\"submit\" value='Submit Order'> -->
							</td>
						</tr>";
					}else{
						echo "
						<tr>
							<td colspan='5' align='center'>
								<b></br>Your order of&nbsp;".CURRENCY. number_format($intTotal +  $intVatTotal, 2, '.', '')."&nbsp;
								is &nbsp;".CURRENCY. number_format(MINIMUM_ORDER - $intTotal +  $intVatTotal, 2, '.', '')."&nbsp;
								short of the minimum order value of ".CURRENCY. number_format(MINIMUM_ORDER,2, '.', '').".</b>
							</td>
						</tr>";						
				 	}					
				}else{
					echo "
					<tr>
						<td colspan='5'><b>You haven't selected anything to buy.</b></td>
					</tr>";
				} 
		//}
echo "	
			
		</table> 
		</form>
	</td>
</tr>";
?>


